home *** CD-ROM | disk | FTP | other *** search
/ PC Open 107 / PC Open 107 CD 1.bin / CD1 / INTERNET / EMAIL / pop file / setup.exe / insert.pl < prev    next >
Encoding:
Perl Script  |  2004-11-29  |  3.3 KB  |  110 lines

  1. #!/usr/bin/perl
  2. # ----------------------------------------------------------------------------
  3. #
  4. # insert.pl --- Inserts a mail message into a specific bucket
  5. #
  6. # Copyright (c) 2001-2004 John Graham-Cumming
  7. #
  8. #   This file is part of POPFile
  9. #
  10. #   POPFile is free software; you can redistribute it and/or modify
  11. #   it under the terms of the GNU General Public License as published by
  12. #   the Free Software Foundation; either version 2 of the License, or
  13. #   (at your option) any later version.
  14. #
  15. #   POPFile is distributed in the hope that it will be useful,
  16. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. #   GNU General Public License for more details.
  19. #
  20. #   You should have received a copy of the GNU General Public License
  21. #   along with POPFile; if not, write to the Free Software
  22. #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. #
  24. # ----------------------------------------------------------------------------
  25.  
  26. use strict;
  27. use lib defined($ENV{POPFILE_ROOT})?$ENV{POPFILE_ROOT}:'./';
  28. use POPFile::Loader;
  29.  
  30. my $code = 0;
  31.  
  32. if ( $#ARGV > 0 ) {
  33.  
  34.     # POPFile is actually loaded by the POPFile::Loader object which does all
  35.     # the work
  36.  
  37.     my $POPFile = POPFile::Loader->new();
  38.  
  39.     # Indicate that we should create not output on STDOUT (the POPFile
  40.     # load sequence)
  41.  
  42.     $POPFile->debug(0);
  43.     $POPFile->CORE_loader_init();
  44.     $POPFile->CORE_signals();
  45.     $POPFile->CORE_load( 1 );
  46.     $POPFile->CORE_link_components();
  47.     $POPFile->CORE_initialize();
  48.  
  49.     my $bucket = shift @ARGV;
  50.  
  51.     my @files;
  52.  
  53.     if ($^O =~ /linux/) {
  54.         @files = @ARGV[0 .. $#ARGV];
  55.     } else {
  56.         @files = map { glob } @ARGV[0 .. $#ARGV];
  57.     }
  58.  
  59.     @ARGV = ();
  60.  
  61.     if ( $POPFile->CORE_config() ) {
  62.  
  63.         # Prevent the tool from finding another copy of POPFile running
  64.  
  65.         my $c = $POPFile->get_module('POPFile::Config');
  66.         $c->config_( 'piddir', $c->config_( 'piddir' ) . 'insert.pl.' );
  67.  
  68.         $POPFile->CORE_start();
  69.  
  70.         # TODO: interface violation
  71.         $c->{save_needed__} = 0;
  72.  
  73.         my $b = $POPFile->get_module('Classifier::Bayes');
  74.         my $session = $b->get_session_key( 'admin', '' );
  75.  
  76.         # Check for the existence of each file first because the API
  77.         # call we use does not care if a file is missing
  78.  
  79.         foreach my $file (@files) {
  80.             if ( !(-e $file) ) {
  81.                 print STDERR "Error: File `$file' does not exist, insert aborted.\n";
  82.                 $code = 1;
  83.                 last;
  84.             }
  85.         }
  86.  
  87.         if ( $code == 0 ) {
  88.             if ( !$b->is_bucket( $session, $bucket ) ) {
  89.                 print STDERR "Error: Bucket `$bucket' does not exist, insert aborted.\n";
  90.                 $code = 1;
  91.             } else {
  92.                 $b->add_messages_to_bucket( $session, $bucket, @files );
  93.                 print "Added ", $#files+1, " files to `$bucket'\n";
  94.             }
  95.         }
  96.  
  97.         $b->release_session_key( $session );
  98.         $POPFile->CORE_stop();
  99.     }
  100. } else {
  101.     print "insert.pl - insert mail messages into a specific bucket\n\n";
  102.     print "Usage: insert.pl <bucket> <messages>\n";
  103.     print "       <bucket>           The name of the bucket\n";
  104.     print "       <messages>         Filename of message(s) to insert\n";
  105.     $code = 1;
  106. }
  107.  
  108. exit $code;
  109.  
  110.